<?php


namespace App\Server;


use Intervention\Image\Facades\Image;
use OSS\OssClient;

/**
 * 阿里云OSS
 * Class AliOSS
 * @package App\Server
 */
class AliOSS
{
    protected $server = null;

    /**
     * AliOSS constructor.
     * @throws \OSS\Core\OssException
     */
    function __construct()
    {
        $this->server = new OssClient(env('ALIYUN_ACCESS_ID'), env('ALIYUN_ACCESS_KEY'), env('ALIYUN_ENDPOINT'));
    }

    /**
     * 生成缩略图
     * @param $imgPath
     * @return null
     */
    public function createThumb($imgPath)
    {
        $content = file_get_contents(img_url($imgPath));
        $imgName = explode('.', $imgPath);
        $thumbImg = $imgName[0] . '_thumb.' . $imgName[1];
        $img = Image::make($content);
        $image = (string) $img->fit(320, 320, function ($constraint) {
            $constraint->upsize();
        })->encode($imgName[1]);
        return $this->putObject($thumbImg, $image);
    }


    /**
     * 生成Banner尺寸图  长宽比 15：8
     * @param $imgPath
     * @param $wRate
     * @param $hRate
     * @return null
     */
    public function createBanner($imgPath, $wRate = 15, $hRate = 8)
    {
        $content = file_get_contents(img_url($imgPath));
        $imgName = explode('.', $imgPath);
        $bannerImg = $imgName[0] . '_banner.' . $imgName[1];
        $img = Image::make($content);
        $w = $img->width() < $img->height() ? $img->width() : $img->height() / $hRate * $wRate;
        $h = $img->width() > $img->height() ? $img->height() : $img->width() / $wRate * $hRate;
        $image = (string) $img->fit(round($w), round($h), function ($constraint) {
            $constraint->upsize();
        })->encode($imgName[1]);
        return $this->putObject($bannerImg, $image);
    }

    /**
     * 上传图片
     * @param $fileName  图片名
     * @param $content   图片二进制
     * @param $base64   图片二进制
     * @return null
     */
    public function putObject($fileName, $content, $base64 = false)
    {
        return $this->server->putObject(env('ALIYUN_BUCKET'), $fileName, $content);
    }


    /**
     * 上传base64图片
     * @param $content
     * @return mixed
     */
    public function putBase64Object($content)
    {
        $mime = explode('/', getimagesize($content)['mime'])[1];
        $fileName = 'images/' . date('YmdHis') . uniqid(). '.' . $mime;

        $img = explode(',', $content);
        $content = base64_decode(end($img));

        $res = $this->server->putObject(env('ALIYUN_BUCKET'), $fileName, $content);
        $url = $res['info']['url'];
        if (strpos($url, 'https') === false) {
            $url = str_replace('http', 'https', $url);
        }
        return $url;
    }


    /**
     * 拼接缩略图地址
     * @param $img
     * @return string
     */
    public static function getThumbName($img)
    {
        if (!$img) {
            return '';
        }
        $imgName = explode('.', $img);
        return $imgName[0] . '_thumb.' . $imgName[1];
    }

    /**
     * 拼接banner图地址
     * @param $img
     * @return string
     */
    public static function getBannerName($img)
    {
        if (!$img) {
            return '';
        }
        $imgName = explode('.', $img);
        return $imgName[0] . '_banner.' . $imgName[1];
    }
}